package utils

import (
	"crypto/md5"
	"encoding/hex"
	"io"
)

// SumData 计算数据的MD5摘要
func SumData(reader io.Reader) (string, error) {
	md5Hash := md5.New()
	buf := make([]byte, 1024)
	for {
		n, err := reader.Read(buf)
		if err != nil && err != io.EOF {
			return "", err
		}

		if n == 0 {
			break
		}

		md5Hash.Write(buf[:n])
	}
	return hex.EncodeToString(md5Hash.Sum(nil)), nil
}
